<template>
  <div class="search-bar-container active">
    <img src="https://cdn4.iconfinder.com/data/icons/evil-icons-user-interface/64/magnifier-512.png" alt="magnifier"
         class="magnifier" @click="myMagnifier" />
    <input type="text" v-model="article.title" @keyup.enter="serchArticleByTitle()" class="input" placeholder="文章标题" />
    <img src="/wx.png" alt="mic-icon"
         class="mic-icon" />
  </div>

</template>
<script setup >
import {onUnmounted, ref} from "vue";
import emitter from "@/utils/emitter.js"
import {getArticleByTitle} from "@/api/article.js"
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
const article=ref({
  title:null,
  pageCurrent:null,
  pageSize:null
})


async function serchArticleByTitle(){
  if (article.value.title === '' || article.value.title == null){
    ElMessage.info("搜索不能为空")
    return
  }
  const {data} = await getArticleByTitle(article.value)
  const res ={
    data:data,
    title:article.value.title
  }
  //将得到的文章分页数据返回
  emitter.emit('send-articleList',res)

}
function myMagnifier(){
  router.push("/")
  const searchBarContainerEl = document.querySelector(".search-bar-container");
  searchBarContainerEl?.classList.toggle("active");
}

//绑定事件 类似于事件监听
emitter.on('send-pageParam',(da)=>{
  article.value.pageSize = da.pageSize;
  article.value.pageCurrent = da.pageCurrent
})
emitter.on('remove-title',()=>{
  article.value.title = null
})
onUnmounted(()=>{
  emitter.off('send-pageParam')
})

</script>
<style scoped>

.search-bar-container {
  display: flex;
  align-items: center;
  background-color: aliceblue;
  padding: 0;
  width: 230px;
  height: 50px;
  border-radius: 50px;
  /* box-shadow: 6px 6px 10px rgba(0, 0, 0, 0.2),
   -6px -6px 10px rgba(255, 255, 255, 0.7);*/
  margin: 2%;
  position: relative;
  transition: width 1.5s;
}

.magnifier {
  width: 25px;
  cursor: pointer;
  position: absolute;
  left: 13px;
}

.mic-icon {
  width: 30px;
  position: absolute;
  right: 10px;
  transition: width 0.4s;
  transition-delay: 1s;
}

.input {
  background-color: transparent;
  border: none;
  margin: 10px 50px;
  width: 100%;
  outline: none;
  color: rgb(100, 100, 100);
  transition: width 1s;
  transition-delay: 0.5s;
  font-weight: bold;
}

.active.search-bar-container {
  width: 50px;
}

.active .input {
  width: 0;
}

.active .mic-icon {
  width: 0;
}
</style>